package RJZ.Rainbow.Controller;

import RJZ.Rainbow.Config.Information;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

@Component
public class SubPanel extends Panel {
    @Autowired
    OrderController orderController;
    @Autowired
    Information imfor;
    JTextField textInPanel = new JTextField(30);
    JLabel tips = new JLabel("2.请扫描投件码");

    StringBuffer stringBuffer = new StringBuffer();

    public SubPanel() {
        this.add(tips);
        this.add(textInPanel);
        setKeyBoardListener();
        setVisible(true);
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        setSize(d.width, d.height/2);
        textInPanel.setFocusable(true);
        addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                textInPanel.setText("");
                //textInPanel.requestFocus();
            }

            @Override
            public void focusLost(FocusEvent e) {
                stringBuffer = new StringBuffer();
            }
        });
    }

    public void focus() {
        textInPanel.requestFocus();
    }

    public void setKeyBoardListener() {
        textInPanel.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
            }

            @Override
            public void keyPressed(KeyEvent e) {

            }

            @Override
            public void keyReleased(KeyEvent e) {
                //System.out.println(e.getKeyChar());
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    if (textInPanel.getText().equals(imfor.getCode())) {
                        System.exit(0);
                    } else {
                        String message1 = "{" +
                                "\"Cop\":" + "\"110\","
                                + "\"box_id\":" + "\"" + imfor.getId() + "\","
                                + "\"code\":" + "\"" + textInPanel.getText() + "\""
                                + "}";
                        String message = message1 + '\n';
                        orderController.getCh().writeAndFlush(message);
                        System.out.println("发送成功\n");
                        String up = "{" +
                                "\"Cop\":" + "\"181\"," +
                                "\"box_id\":\"" + imfor.getId() + "\"," +
                                "\"boxlog\":" + message1 +
                                "}" + '\n';
                        orderController.getCh().writeAndFlush(up);
                        textInPanel.setText("");
                        stringBuffer = new StringBuffer();
                    }
                } else {
                    stringBuffer.append(e.getKeyChar());
                    textInPanel.setText(stringBuffer.toString());
                }
            }
        });

    }


}
